package com.training.android.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class CalculatorActivity extends Activity {
	/** Called when the activity is first created. */

	Button zero, one, two, three, four, five, six, seven, eight, nine, dot;
	Button equals, plus, minus, multiply, divide, mDivide;
	Button clear;
	EditText inputEditText;

	boolean performOperation = false, tempPerformOperationClicked = false,
			newNumber = false, equalsClicked = false, gotMathException = false;
	int action;
	double firstNumber, secondNumber;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initButtons();
	}

	public void initButtons() {

		inputEditText = (EditText) findViewById(R.id.inputEditText);

		zero = (Button) findViewById(R.id.zeroButton);
		zero.setOnClickListener(numberListener);

		one = (Button) findViewById(R.id.oneButton);
		one.setOnClickListener(numberListener);

		two = (Button) findViewById(R.id.twoButton);
		two.setOnClickListener(numberListener);

		three = (Button) findViewById(R.id.threeButton);
		three.setOnClickListener(numberListener);

		four = (Button) findViewById(R.id.fourButton);
		four.setOnClickListener(numberListener);

		five = (Button) findViewById(R.id.fiveButton);
		five.setOnClickListener(numberListener);

		six = (Button) findViewById(R.id.sixButton);
		six.setOnClickListener(numberListener);

		seven = (Button) findViewById(R.id.sevenButton);
		seven.setOnClickListener(numberListener);

		eight = (Button) findViewById(R.id.eightButton);
		eight.setOnClickListener(numberListener);

		nine = (Button) findViewById(R.id.nineButton);
		nine.setOnClickListener(numberListener);

		dot = (Button) findViewById(R.id.dotButton);
		dot.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				dotClicked(v);
			}
		});

		equals = (Button) findViewById(R.id.equalsButton);
		equals.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				equalsClicked(v);
			}
		});

		plus = (Button) findViewById(R.id.plusButton);
		plus.setHint("1");
		plus.setOnClickListener(operationListener);

		minus = (Button) findViewById(R.id.minusButton);
		minus.setHint("2");
		minus.setOnClickListener(operationListener);

		multiply = (Button) findViewById(R.id.multiplyButton);
		multiply.setHint("3");
		multiply.setOnClickListener(operationListener);

		divide = (Button) findViewById(R.id.divideButton);
		divide.setHint("4");
		divide.setOnClickListener(operationListener);

		mDivide = (Button) findViewById(R.id.mdivideButton);
		mDivide.setHint("5");
		mDivide.setOnClickListener(operationListener);

		clear = (Button) findViewById(R.id.clearButton);
		clear.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				performOperation = false;
				tempPerformOperationClicked = false;
				equalsClicked = false;
				firstNumber = 0;
				secondNumber = 0;
				inputEditText.setText("");
				gotMathException = false;
			}
		});
	}

	View.OnClickListener operationListener = new View.OnClickListener() {
		Button temp;
		String currentText;

		@Override
		public void onClick(View v) {
			if(gotMathException) {
				return;
			}
			temp = (Button) findViewById(v.getId());
			currentText = inputEditText.getText().toString();
			equalsClicked = false;

			if (currentText.length() > 0) {
				firstNumber = Double.valueOf(currentText);
				action = Integer.parseInt(temp.getHint().toString());
				performOperation = true;
				tempPerformOperationClicked = true;
			}
		}
	};

	View.OnClickListener numberListener = new View.OnClickListener() {
		Button temp;
		String currentText;

		@Override
		public void onClick(View v) {
			if(gotMathException) {
				return;
			}
			temp = (Button) findViewById(v.getId());
			currentText = inputEditText.getText().toString();
			if (tempPerformOperationClicked || equalsClicked) {
				inputEditText.setText(temp.getText());
				tempPerformOperationClicked = false;
				equalsClicked = false;
			} else {
				inputEditText.setText(currentText + temp.getText());
			}
		}
	};

	public void dotClicked(View v) {
		if(gotMathException) {
			return;
		}
		String currentText;
		currentText = inputEditText.getText().toString();
		if (currentText.indexOf(".") == -1 && currentText.length() > 0) {
			inputEditText.setText(currentText + ".");
		}
	}

	public void equalsClicked(View v) {
		if(gotMathException) {
			return;
		}
		String currentText;
		if (performOperation) {
			currentText = inputEditText.getText().toString();
			if (!equalsClicked) {
				secondNumber = Double.valueOf(currentText);
			}
			calculateResult();
		}
		tempPerformOperationClicked = false;
		equalsClicked = true;
		// performOperation = false;
	}

	public void calculateResult() {
		// Toast.makeText(getApplicationContext(), String.valueOf(firstNumber),
		// Toast.LENGTH_SHORT).show();
		try {
			switch (action) {
			case 1:// add
				firstNumber = firstNumber + secondNumber;
				break;
			case 2:// subtract
				firstNumber = firstNumber - secondNumber;
				break;
			case 3:// multiply
				firstNumber = firstNumber * secondNumber;
				break;
			case 4:// divide
				if (secondNumber != 0) {
					firstNumber = firstNumber / secondNumber;
				} else {
					inputEditText.setText("can not divide by zero");
					gotMathException = true;
					return;
				}
				break;
			case 5:// modulo divide
				if (secondNumber != 0) {
					firstNumber = firstNumber % secondNumber;
				} else {
					inputEditText.setText("can not divide by zero");
					gotMathException = true;
					return;
				}
				break;
			}
			inputEditText.setText(String.valueOf(firstNumber));
		} catch (Exception e) {
			Log.e("exception", e.getMessage());
		}
	}
}